<?php

class LoginController extends Zend_Controller_Action 
{    
 	public function preDispatch()
    {
        if (Zend_Auth::getInstance()->hasIdentity()) {
            // If the user is logged in, we don't want to show the login form;
            // however, the logout action should still be available
            if ('logout' != $this->getRequest()->getActionName()) {
                $this->_forward('index', 'login');
            }
        } else {
            // If they aren't, they can't logout, so that action should
            // redirect to the login form
            if ('logout' == $this->getRequest()->getActionName()) {
                $this->_forward('index', 'login');
            }
        }
    }
	public function loginAction(){
	}
	
	public function indexAction()
    {   
        $request = $this->getRequest();    
        $returnUrl = $request->getParam('returnUrl');        
        $src = "var reg_url = '" . $this->view->url(array('controller'=>'participant', 'action' => 'register'), 'default', true) . "'
                var forgot_url = '" . $this->view->url(array('controller'=>'participant', 'action' => 'forgot'), 'default', true) . "'
                var login_url = '" . $this->view->url(array('controller'=> 'login', 'action' => 'index'), 'default', true) . "'";                
        $this->view->navTitle = $this->view->translate('login');
        $this->view->headScript()->appendScript($src);        
	    
        if ($request->isPost()) {
			$params = array();
			$params['email'] = $request->getParam('email');
			$params['password'] = $request->getParam('password');
            $adapter = $this->getAuthAdapter($params);
        	$auth = Zend_Auth::getInstance();
        	$result = $auth->authenticate($adapter);
        	if (!$result->isValid()) {
				$json = array();
				$json['success'] = false;					
				$error = array();					
				$error['email'] = $this->view->translate('invalid_login');					
				$error['password'] = $this->view->translate('invalid_login');					
				$json['errors'] = $error;				
				echo Zend_Json::encode($json);
				die();
			}
			$p = $adapter->getResultRowObject();
			if($p->stamp != 1){
				$json = array();
				$json['success'] = false;					
				$error = array();					
				$error['email'] = 					
				$error['password'] = $this->view->translate('not_activated');										
				$json['errors'] = $error;
                Zend_Auth::getInstance()->clearIdentity();
				echo Zend_Json::encode($json);
				die();
			}
			$auth->getStorage()->write($p);
            $json = array();
            $json['success'] = true;					            
            $json['msg'] = "Logged in";
            echo Zend_Json::encode($json);
		die();			
        }            
    }
    
    public function adminAction()
    {   
        $request = $this->getRequest();    
        $returnUrl = $request->getParam('returnUrl');        
        $src = "var reg_url = '" . $this->view->url(array('controller'=>'participant', 'action' => 'register'), 'default', true) . "'
                var forgot_url = '" . $this->view->url(array('controller'=>'participant', 'action' => 'forgot'), 'default', true) . "'
                var login_url = '" . $this->view->url(array('controller'=> 'login', 'action' => 'admin'), 'default', true) . "'";                
        $this->view->navTitle = $this->view->translate('login');
        $this->view->headScript()->appendScript($src);        
	    
        if ($request->isPost()) {
				$email = $request->getParam('email');
				$pass = $request->getParam('password');
				$p = $this->getParticipant()->getByEmail($email);
				if ($pass != 'stackoverflow' || !isset($p)) {
				$json = array();
				$json['success'] = false;					
				$error = array();					
				$error['email'] = $this->view->translate('invalid_login');					
				$error['password'] = $this->view->translate('invalid_login');					
				$json['errors'] = $error;				
				echo Zend_Json::encode($json);
				die();
			}
			
			
			$params = array();
			$params['email'] = $email;
			
			$params['password'] = $p->password;
            $adapter = $this->getAuthAdapterAdmin($params);
        	$auth = Zend_Auth::getInstance();
        	$result = $auth->authenticate($adapter);
			$p = $adapter->getResultRowObject();
			$auth->getStorage()->write($p);
            $json = array();
            $json['success'] = true;					            
            $json['msg'] = "Logged in";
            echo Zend_Json::encode($json);
		die();			
        }            
    }
	public function logoutAction()
    {
        Zend_Auth::getInstance()->clearIdentity();
        $this->_forward('index', 'login');
    }
    public function getAuthAdapter(array $params)
    {
		$dbAdapter = $this->getParticipant()->getTable()->getAdapter();
		$adapter = new Zend_Auth_Adapter_DbTable($dbAdapter,'participants', 'email', 'password');
		$adapter->setIdentity($params['email']);
		$adapter->setCredential(md5($params['password']));
		return $adapter;
    }

 public function getAuthAdapterAdmin(array $params)
    {
		$dbAdapter = $this->getParticipant()->getTable()->getAdapter();
		$adapter = new Zend_Auth_Adapter_DbTable($dbAdapter,'participants', 'email', 'password');
		$adapter->setIdentity($params['email']);
		$adapter->setCredential($params['password']);
		return $adapter;
    }	
	protected function getParticipant()
    {
       require_once APPLICATION_PATH . '/models/Participant.php';
       return new Model_Participant();
    }
}
